/**
 * 系统框架基础服务
 * by @author lms 2022.07.21
 */
package ms.core.gateway.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import ms.core.common.ApiResult;
import ms.core.common.RequestBody;
import ms.core.common.consts.SysErr;
import ms.core.gateway.bean.SysParamBean;
import ms.core.gateway.dto.SysFuncDTO;
import ms.core.gateway.response.SysParamVO;
import ms.core.gateway.tool.SessionTool;
import ms.core.gateway.utils.DicConstUtils;
import ms.core.gateway.utils.SysFuncUtils;
import ms.core.gateway.utils.SysParamUtils;
import ms.core.tool.DateTimeTool;
import ms.core.tool.StrTool;

public class SystemService {

	/**
	 * 接口：<div style="color:red"><b> getTime </b></div><p>
	 * 功能: 获取系统时间<p>
	 *
	 * @param req
	 * @param resp
	 * @param body
	 * @param sid
	 * @return
	 */
	public ApiResult getTime(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid) {
		Date now = new Date();
		Map<String, Object> ret = new HashMap<>();
		ret.put("unixTime", now.getTime());
		ret.put("strTime", DateTimeTool.dateToDateTimeString(now));
		return new ApiResult(true).setData(ret);
	}
	
	/**
	 * 接口：<div style="color:red"><b> webInfo </b></div><p>
	 * 功能: 查询平台web信息<p>
	 * 
	 * @param req
	 * @param resp
	 * @param body
	 * @param sid
	 * @return
	 */
	public ApiResult webInfo(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid) {
		return new ApiResult(true).setData(SysParamUtils.getParam(SysParamBean.TYPE_WEB));
	}

	/**
	 * 接口：<div style="color:red"><b> sysInfo </b></div><p>
	 * 功能: 查询平台框架信息<p>
	 * 
	 * @param req
	 * @param resp
	 * @param body
	 * @param sid
	 * @return
	 */
	public ApiResult sysInfo(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid) {
		return new ApiResult(true).setData(SysParamUtils.getParam(SysParamBean.TYPE_SYS));
	}

	/**
	 * 接口：<div style="color:red"><b> getDict </b></div><p>
	 * 功能: 查询业务代码对应常量字典<p>
	 * 
	 * @param req
	 * @param resp
	 * @param body={"code":"业务表代码","name":"常量名"}
	 * @param sid
	 * @return
	 */
	public ApiResult getConsts(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid) {
		RequestBody rb = RequestBody.getInstance(body);
		String code = rb.getStrValue("code", null);
		String name = rb.getStrValue("name", null);
		if (StrTool.isBlank(code) || StrTool.isBlank(name))
			return new ApiResult(false, SysErr.SYS_PARAM_ERR);

		return new ApiResult(true).setData(DicConstUtils.getConst(code, name, false));
	}

	/**
	 * 接口：<div style="color:red"><b> edtParam </b></div><p>
	 * 功能：修改系统参数<p>
	 * 
	 * @param req
	 * @param resp
	 * @param body={"pcode":"参数名","pvalue":"参数值"}<p>
	 * @param sid
	 * @return
	 */
	public ApiResult editParam(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid) {
		RequestBody rb = RequestBody.getInstance(body);
		String pcode  = rb.getStrValue("pcode", null);
		String pvalue = rb.getStrValue("pvalue", null);
		if (StrTool.isBlank(pcode) || StrTool.isBlank(pvalue))
			return new ApiResult(false, SysErr.SYS_PARAM_ERR);

		SysParamUtils utils = new SysParamUtils();
		utils.updateParam(pcode, pvalue);
		return new ApiResult(true);
	} 

	/**
	 * 接口：<div style="color:red"><b> typParam </b></div><p>
	 * 功能：查询参数类型 <p>
	 * 
	 * @param req
	 * @param resp
	 * @param body
	 * @param sid
	 * @return
	 */
	public ApiResult paramType(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid) {
		return new ApiResult(true).setData(DicConstUtils.getConst("SYS0", "ptype", true));
	}

	/**
	 * 接口：<div style="color:red"><b> qryParam </b></div><p>
	 * 功能：查询系统参数 <p>
	 * 
	 * @param req
	 * @param resp
	 * @param body={"pcode":"参数名","ptype":参数类型(0-框架;1-平台参数;2-邮局设置...)}
	 * @param sid
	 * @return
	 */
	public ApiResult queryParam(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid) {
		RequestBody rb = RequestBody.getInstance(body);
		String pcode = rb.getStrValue("pcode", null);
		int ptype = rb.getIntValue("ptype", -1);

		SysParamUtils utils = new SysParamUtils();
		List<SysParamVO> rets = utils.queryParams(pcode, ptype);
		return new ApiResult(true).setData(rets);
	} 

	/**
	 * 接口：<div style="color:red"><b> getFunc </b></div><p>
	 * 功能：根据当前登录用户类型,获取该类型用户的系统所有功能. <p>
	 * 场景: 配置角色时加载菜单功能. <p>
	 * 
	 * @param req
	 * @param resp
	 * @param body
	 * @param sid
	 * @return
	 */
	public ApiResult loadFuncs(HttpServletRequest req, HttpServletResponse resp, Map<String, Object> body, String sid) {
		List<SysFuncDTO> rets = SysFuncUtils.loadModules(SessionTool.getCtype(sid));
		return new ApiResult(true).setData(rets);
	} 
}
